
import React,{Component} from 'react'
import PropTypes from 'prop-types'
import ReactDom, {render}  from 'react-dom'

class MessageComs extends Component{
  static defaultProps = {
    text: '',
    code: 0
  }
  componentDidMount(){
    let _m = msg()
    let clear = setTimeout(()=>{
      _m.destroy()
      clearTimeout(clear)
    },3000)
  }
  render(){
    let {text,code} = this.props
    return (  
      <div className={`alert-tip level-${code}`}>
        <p>{text}</p>
      </div>
    )
  }
}

const msg = ()=>{
  let divKey='div_key_'
  return {
    info:(text,code=0)=>{
      let div = document.getElementById(divKey)
      if(!div){
        let div = document.createElement('div')
        div = document.createElement('div')
        div.id = divKey
        document.body.appendChild(div);
        render(
          <MessageComs text={text} divKey={divKey} code={code}></MessageComs>,div
        )
      }
    },
    destroy:(key)=>{
      let div = document.getElementById(divKey)
      if(div){
        ReactDom.unmountComponentAtNode(div)
        document.body.removeChild(div);
      }
    }
  }
}
export default msg()

